package com.zb.zeus.common.datasource.configure;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zb.zeus.common.datasource.annotation.ZeusMapperScan;
import com.zb.zeus.common.security.context.UserContextHolder;
import com.zb.zeus.system.api.model.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author fujianjian
 * @since 2021/7/9 14:54
 */
@ZeusMapperScan
@Configuration
public class MybatisPlusConfiguration {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 无参构造，会支持mybaits-plus已经支持的所有数据库分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    @ConditionalOnMissingBean(MetaObjectHandler.class)
    public MetaObjectHandler zeusMetaObjectHandler() {
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {

                Date currentTime = new Date();
                LoginUser loginUser = UserContextHolder.getLoginUser();
                String adNum = loginUser.getAdNum();
                String username = loginUser.getUsername();
                this.setFieldValByName("createBy", adNum, metaObject);
                this.setFieldValByName("createName", username, metaObject);
                if (getFieldValByName("createTime", metaObject) == null) {
                    this.setFieldValByName("createTime", currentTime, metaObject);
                }
                this.setFieldValByName("updateBy", adNum, metaObject);
                this.setFieldValByName("updateName", username, metaObject);
                if (getFieldValByName("updateTime", metaObject) == null) {
                    this.setFieldValByName("updateTime", currentTime, metaObject);
                }
                this.setFieldValByName("dr", 0, metaObject);
                this.setFieldValByName("version", 0, metaObject);
                this.setFieldValByName("edition", 0, metaObject);

            }

            @Override
            public void updateFill(MetaObject metaObject) {
                Date currentTime = new Date();
                LoginUser loginUser = UserContextHolder.getLoginUser();
                String adNum = loginUser.getAdNum();
                String username = loginUser.getUsername();
                this.setFieldValByName("updateName", username, metaObject);
                this.setFieldValByName("updateBy", adNum, metaObject);

                if (getFieldValByName("updateTime", metaObject) == null) {
                    this.setFieldValByName("updateTime", currentTime, metaObject);
                }
            }
        };
    }


}
